<?php

namespace App\Http\Controllers;
use App\Models\Video;
use Illuminate\Support\Facades\DB;
use App\Models\Expert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;

class ExpertController extends Controller
{
    public function admin()
    {
        return view('admin.admin-index');
    }
    public function expert(){
        $infos = Expert::paginate(6);
        return view('front.expert')->with('infos',$infos);
    }
    public function conf(){
        $latestStartTime = DB::table('videos')->min('begin');
        $oldestVideo = DB::table('videos')->where('begin', '=', $latestStartTime)->first();
        $currentVideo = Video::with('experts')->findOrFail($oldestVideo->id);
        return view('front.conf')->with('latestStartTime', $latestStartTime)->with('currentVideo',$currentVideo);

    }
    public function index()
    {

        $oExperts = Expert::paginate(10);
        return view('admin.admin-expert')->with('oExperts',$oExperts);
    }
    public function create()
    {

    }
    public function add()
    {
        return view('admin.admin-addexpert');
    }
    public function upload(Request $request)
    {
        $data = $request->input();
        if ($request->hasFile('upload_file'))
        {
            $file = $request->file('upload_file');
            $filename = time().'.'.$file->getClientOriginalExtension();//获取文件后缀
            $file->move(public_path()."/upload",$filename);//文件转移
            $data['upload_file'] = '/upload/'.$filename;
            return json_encode(['success'=>true,'msg'=>'上传成功']);
        }else{
            return json_encode(['success'=>false,'msg'=>'上传失败，设为默认值']);
        }
    }

    public function store(Request $request)//增加管理员用户功能，已实现
    {
        $data = $request->input();

        $rules = [
            'name' => 'required|string|max:255',
            'thumb' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
            'department' => 'required|string|max:255',
            'chenghao' => 'required|string|max:255',
            'job' => 'required|string|max:255',
            'hospital' => 'required|string|max:255',
            'introduction' => 'required|string',
            'education' => 'required|string'
        ];
        $message = [
            'name.required'=>'用户姓名不能为空，必须填写',
            'thumb.required'=>'图片不能为空，必须上传',
            'thumb.image'=>'上传的文件必须为图片',
            'thumb.mimes'=>'图片格式必须为jpeg,png,jpg,gif的格式',
            'chenghao.required'=>'职称不能为空，必须填写',
            'job.required'=>'职务不能为空，必须填写',
            'department.required'=>'科室不能为空，必须填写',
            'hospital.required'=>'医院不能为空，必须填写',
            'introduction.required'=>'简介不能为空，必须填写',
            'education.required'=>'教育不能为空，必须填写',
        ];
        $validate = Validator::make($data,$rules, $message);
        if($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{
            Expert::create($data);
            return json_encode(['success'=>true,'msg'=>'成功提交']);
        }
//        return redirect('adm.expert');
    }



    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $expert = Expert::findOrFail($id);
        $videos = $expert->videos;
        $relatedExperts = collect(); // 创建一个空的集合对象
        foreach ($videos as $video) {
            $experts = $video->experts;
            // 将所有专家的 id 加入到 $relatedExperts 集合中
            $relatedExperts = $relatedExperts->merge($experts->pluck('id'));
        }
        $relatedExpertIds = $relatedExperts->unique()->values()->all();
        $Oexperts = Expert::whereIn('id', $relatedExpertIds)
            ->whereNotIn('id', [$id])
            ->get();
        return view('front.expert-show')
            ->with('infos', $expert)
            ->with('Oexperts', $Oexperts);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $oExpert = Expert::find($id);
        return view('admin.admin-editexpert')->with('oExpert',$oExpert);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validatedData = $request->input();
        $rules = [
            'name' => 'required|string|max:255',
            'thumb' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
            'department' => 'required|string|max:255',
            'chenghao' => 'required|string|max:255',
            'job' => 'required|string|max:255',
            'hospital' => 'required|string|max:255',
            'introduction' => 'required|string',
            'education' => 'required|string'
        ];
        $message = [
            'name.required'=>'用户姓名不能为空，必须填写',
            'chenghao.required'=>'职称不能为空，必须填写',
            'job.required'=>'职务不能为空，必须填写',
            'department.required'=>'科室不能为空，必须填写',
            'hospital.required'=>'医院不能为空，必须填写',
            'introduction.required'=>'简介不能为空，必须填写',
            'education.required'=>'教育不能为空，必须填写',
        ];
        $validate = Validator::make($validatedData,$rules, $message);
        if($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{
            $expert = Expert::find($id);
            $expert->update($validatedData);
            return json_encode(['success'=>true,'msg'=>'成功提交']);
        }
    }



    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)//删除专家功能,已实现
    {
        $data = $request->input();
        $expert = Expert::find($data['id']);
        $expert->delete();
        return redirect('/adm/expert');
    }
}
